package com.tudouji.project.d3.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.d3.domain.D3Order;
import com.tudouji.project.d3.domain.D3OrderItem;
import com.tudouji.project.d3.mapper.D3ToSellformMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.d3.domain.D3ToSellform;
import com.tudouji.project.d3.service.ID3ToSellformService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 平台订单中间Controller
 * 
 * @author tudouji
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/d3/tosellform")
public class D3ToSellformController extends BaseController
{
    @Autowired
    private ID3ToSellformService d3ToSellformService;
    @Autowired
    private D3ToSellformMapper d3ToSellformMapper;

    /**
     * 查询平台订单中间列表
     */
    @GetMapping("/listPc")
    public TableDataInfo listPc(D3ToSellform d3ToSellform)
    {
        //ERP单品数量
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            if (StringUtils.isNotEmpty(d3ToSellform.getBeginTime())) {
                Date date = simpleDateFormat.parse(d3ToSellform.getBeginTime());
//                date =  DateUtils.addDay(date, 1);
                Long l = date.getTime();
                String s = String.valueOf(l);
                d3ToSellform.setBeginTime(s);
            }
            if (StringUtils.isNotEmpty(d3ToSellform.getEndTime())) {
                Date date1 = simpleDateFormat.parse(d3ToSellform.getEndTime());
                date1 =  DateUtils.addDay(date1, 1);
                Long l1 = date1.getTime();
                String s1 = String.valueOf(l1);
                d3ToSellform.setEndTime(s1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer numSum = d3ToSellformMapper.getNumSum(d3ToSellform);
        //ERP各单品数量
        List<D3ToSellform> numSumGroup = d3ToSellformMapper.getNumSumGroup(d3ToSellform);
        //电商订单数量
        List<D3Order> orders = d3ToSellformMapper.preparedForD3Orders(d3ToSellform);
        //电商订单商品数量
        int orderItemProductNum = d3ToSellformMapper.preparedForD3OrderItems(d3ToSellform);

        startPage();
        List<D3ToSellform> list = d3ToSellformService.listPc(d3ToSellform);
        return getDataTable(list,new BigDecimal(numSum == null ? 0: numSum),orders.size(), numSumGroup, orderItemProductNum);
    }

    /**
     * 查询平台订单中间列表
     */
    @GetMapping("/list")
    public TableDataInfo list(D3ToSellform d3ToSellform)
    {
        startPage();
        List<D3ToSellform> list = d3ToSellformService.selectD3ToSellformList(d3ToSellform);
        return getDataTable(list);
    }

    /**
     * 导出平台订单中间列表
     */
    @PreAuthorize("@ss.hasPermi('d3:tosellform:export')")
    @Log(title = "平台订单中间", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(D3ToSellform d3ToSellform)
    {
        List<D3ToSellform> list = d3ToSellformService.selectD3ToSellformList(d3ToSellform);
        ExcelUtil<D3ToSellform> util = new ExcelUtil<D3ToSellform>(D3ToSellform.class);
        return util.exportExcel(list, "tosellform");
    }

    /**
     * 获取平台订单中间详细信息
     */
    @PreAuthorize("@ss.hasPermi('d3:tosellform:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(d3ToSellformService.selectD3ToSellformById(id));
    }

    /**
     * 新增平台订单中间
     */
    @PreAuthorize("@ss.hasPermi('d3:tosellform:add')")
    @Log(title = "平台订单中间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody D3ToSellform d3ToSellform)
    {
        return toAjax(d3ToSellformService.insertD3ToSellform(d3ToSellform));
    }

    /**
     * 修改平台订单中间
     */
    @PreAuthorize("@ss.hasPermi('d3:tosellform:edit')")
    @Log(title = "平台订单中间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody D3ToSellform d3ToSellform)
    {
        return toAjax(d3ToSellformService.updateD3ToSellform(d3ToSellform));
    }

    /**
     * 删除平台订单中间
     */
    @PreAuthorize("@ss.hasPermi('d3:tosellform:remove')")
    @Log(title = "平台订单中间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(d3ToSellformService.deleteD3ToSellformByIds(ids));
    }
}
